<template>
  <div class="login-cont">
    <el-row :gutter="20" class="login-row" align="middle">
      <el-col :span="8" :offset="8">
        <el-card shadow="always" :body-style="{ padding: '20px' }">
          <template #header>
            <div>
              <span>XXX家具管理系统</span>
            </div>
          </template>
          <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="ruleForm.password"
                type="password"
                autocomplete="off"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm(ruleFormRef)">
                登录
              </el-button>
              <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { useAccount } from "../../store/user";
import { useRouter } from "vue-router";
const account = useAccount();
const router = useRouter(); //Hook方法

const ruleFormRef = ref();

//表单数据包
const ruleForm = reactive({
  username: "三丰",
  password: "123",
});

// 表单验证规则
const rules = reactive({
  username: [{ required: true, trigger: "blur" }],
  password: [{ required: true, trigger: "blur" }],
});

//表单提交事件
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      console.log("submit!"); //表单验证通过
      let data = await account.userLoginAction(ruleForm);
      ElMessage(data);
      if (data.type == "success") {
        router.push("/");
      }
    } else {
      console.log("error submit!"); //表单验证失败
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="scss" scoped>
.login-cont {
  height: 100vh;
  background-image: url(../../assets/th.webp);
  background-size: cover;
}
.login-cont::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  backdrop-filter: blur(10px); /*模糊半径 */
}
.login-row {
  height: 100vh;
}
.el-card {
  background-color: rgba(255, 255, 255, 0.6);
}
</style>
